import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTree {
    public static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode root;

    //遍历创建二叉树
    public TreeNode create() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        root = A;
        return A;//返回不返回都无所谓
    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val +  " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val +  " ");
        inOrder(root.right);
    }

    //后续遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        System.out.print(root.val +  " ");
        postOrder(root.right);
    }

    //前序遍历oj
    public List<Character> preorderTraversal (TreeNode root) {
        List<Character> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        list.add(root.val);
        list.addAll(preorderTraversal(root.left));
        list.addAll(preorderTraversal(root.right));
        return list;
    }

    //获取节点个数
    //时间复杂度 O(N)
    //空间复杂度 O(log2N) 递归要在栈上开辟内存
    public int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = size(root.left);
        int right = size(root.right);
        return 1 + left + right;
    }

    int size = 0;
    public void size2(TreeNode root) {
        if (root == null) {
            return;
        }
        size++;
        size2(root.left);
        size2(root.right);
    }

    //获取叶子节点个数
    public int getLeaf(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        int left = getLeaf(root.left);
        int right = getLeaf(root.right);
        return left + right;
    }

    int leafSize = 0;
    public void getLeaf2(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            leafSize++;
        }
        getLeaf2(root.left);
        getLeaf2(root.right);
    }

    //求第k层节点个数
    public int getKLevelCount(TreeNode root,int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        int left = getKLevelCount(root.left,k - 1);
        int right = getKLevelCount(root.right,k - 1);
        return left + right;
    }

    //获取二叉树的高度
    public int height (TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = height(root.left);
        int right = height(root.right);
        return left > right ? left + 1 : right + 1;
    }

    //判断是不是完全二叉树

    //检测值为value的节点是否存在
    public TreeNode find (TreeNode root,char val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode left = find(root.left,val);
        if (left != null) {
            return left;
        }
        TreeNode right = find(root.right,val);
        if (right != null) {
            return right;
        }
        return null;
    }

    public TreeNode find2 (TreeNode root ,char val) {
        if (root == null) {
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode cur = null;
        queue.offer(root);
        while (!queue.isEmpty()) {
            cur = queue.poll();
            if (cur.val == val) {
                return cur;
            }
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right !=null) {
                queue.offer(cur.right);
            }
        }
        return null;
    }





}